﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace Web.admin.BasicManage.Classes
{
    public partial class CLA_List : System.Web.UI.Page
    {
        ClassService classesService = new ClassService();
        AcademyService academyService = new AcademyService();
        const int pagesize = 20;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RP_Bind();
                DDLBind("全部学院",ddl_ACA);
                if (Session["ClassAcademyselectValue"] != null && Session["ClassAcademyselectValue"].ToString() != "")
                    ddl_ACA.SelectedValue = Session["ClassAcademyselectValue"].ToString();
                FillGradeList();
            }
        }

        protected void FillGradeList()
        {
            ddl_Grade.Items.Clear();
            int year = DateTime.Now.Year;
            ddl_Grade.Items.Add(new ListItem("全部年级", ""));
            for (int i = 0; i < 10; i++)
            {
                ddl_Grade.Items.Add(year.ToString());
                year--;
            }
            if (!String.IsNullOrEmpty((String)Session["ClassGradeSelectedValue"]))
            {
                ddl_Grade.SelectedValue = (String)Session["ClassGradeSelectedValue"];
            }
        }

        /// <summary>
        /// reapeater控件绑定
        /// </summary>
        protected void RP_Bind()
        {
            Pager1.PageSize = pagesize;
            int records = 0;
            if (Session["ClassAcademyCon"] != null && Session["ClassAcademyCon"].ToString() != "")
            {
                if (!string.IsNullOrEmpty(Request.QueryString["select"]))
                {
                    string ddlselect = Request.QueryString["select"];
                    ddl_ACA.SelectedValue = ddlselect;
                    Session["ClassAcademyCon"] += "and CLA_ACADEMY = '" + ddlselect + "'";
                }
                RpList.DataSource = classesService.getPageCompetitions(Session["ClassAcademyCon"].ToString(), " CLA_NAME DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            }
            else
                RpList.DataSource = classesService.getPageCompetitions("", " CLA_NAME DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            RpList.DataBind();
            //初始化分页条,设置样式
            Pager1.SetPage(records);
            Pager1.ShowPageJump = true;
            Pager1.ShowPageLan = 0; //导航条语言:0=默认，1=中文，2=英语

        }
        /// <summary>
        /// 对DropDownList的绑定
        /// </summary>
        /// <param name="firstStr">第一行显示的数据</param>
        /// <param name="ddl">DropDownList控件</param>
        protected void DDLBind(string firstStr, DropDownList ddl)
        {
            if (!string.IsNullOrEmpty(firstStr))
            {
                ddl.Items.Add( new ListItem(firstStr,""));
            }
            List<BoAcademy> boacademy = academyService.getAllAcademies();
            foreach( BoAcademy aca in boacademy)
            {
                ddl.Items.Add(new ListItem(aca.ACA_NAME,aca.ACA_ID));
            }
        }

        /// <summary>
        /// repeater的操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RpOperate(object sender, RepeaterCommandEventArgs e)
        {
            string classId = ((HiddenField)e.Item.FindControl("hf_ClassId")).Value;
            switch (e.CommandName) 
            { 
                case "delClass":
                    classesService.DeleteClass(classId);
                    break;
            }
            RP_Bind();
        }

        protected void FillClasses()
        {
            string selAcamediyId = ddl_ACA.SelectedValue;
            string selGrade = ddl_Grade.SelectedValue;

            Session["ClassAcademyselectValue"] = "";
            Session["ClassAcademyCon"] = "";

            if (selAcamediyId != "")
            {
                Session["ClassAcademyselectValue"] = selAcamediyId;
                Session["ClassAcademyCon"] = " CLA_ACADEMY='" + selAcamediyId + "'";
            }

            if (selGrade == "")
            {
                Session["ClassGradeSelectedValue"] = "";
            }
            else
            {
                Session["ClassGradeSelectedValue"] = selGrade;
                if (Session["ClassAcademyCon"] == null || Session["ClassAcademyCon"].ToString() == "")
                    Session["ClassAcademyCon"] = " CLA_GRADE='" + selGrade + "'";
                else
                    Session["ClassAcademyCon"] += " AND CLA_GRADE='" + selGrade + "'";
            }

            RP_Bind();
        }

        /// <summary>
        /// 下拉列表文本改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddl_ACA_TextChange(object sender ,EventArgs e)
        {
            FillClasses();
        }
        /// <summary>
        /// 批量删除是事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BatchDelete(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < RpList.Items.Count; i++)
            {
                string id = ((HiddenField)RpList.Items[i].FindControl("hf_ClassId")).Value;
                CheckBox cb = (CheckBox)RpList.Items[i].FindControl("CbChoose");
                if (cb.Checked)
                {
                    classesService.DeleteClass(id);
                    j++;
                }
            }
            //int result = classes.DeleteMoreClasses(ClassesId);
            Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='CLA_List.aspx'; </script>");
            return;
        }
    }
}